/*
 * ============================================================================
 *
 *       Filename:  IPRecord.cpp
 *
 *    Description:  Defination of IPRecord class and its operator
 *
 *         Author:  liangqing
 *
 * ============================================================================
 */
#include "IPRecord.h"

namespace {
    inline void output_ip(ostream& os, const unsigned char *ip) {
        std::ostringstream ss;
        ss << (int)ip[3] <<'.'<< (int)ip[2] <<'.'<< (int)ip[1] <<'.'<< (int)ip[0];
        os << std::setw(16) << std::left << ss.str();
    }
}

bool operator<(const Index &a, const Index &b) {
#ifdef USE_SET
    if(IPRecord::compare_ip(a.ip, b.ip) == 0)
        return IPRecord::compare_ip(a._ip, b._ip) < 0;
#endif /* USE_SET */
    return IPRecord::compare_ip(a.ip, b.ip) < 0;
}

ostream& operator<<(ostream& os, const IPRecord& record) {
    output_ip(os, record.begin_ip);
    output_ip(os, record.end_ip);
    
    os << record.region;
    if(record.section[0])
        os << ' ' << record.section;

    return os;
}

ostream& operator<<(ostream& os, const Index& index) {
    os.write((char *)index.ip, sizeof(index.ip));
    os.write(index.offset, sizeof(index.offset));

    return os;
}


